{% extends 'base.html'%}
{% block title %}create credential{% endblock %}
{% load static %}
{%load i18n%}
{% block css%}
<link rel="stylesheet" href="{% static 'plugins/bootstrap-select/css/bootstrap-select.css' %} ">
<link rel="stylesheet" href="{% static 'plugins/json-forms/css/brutusin-json-forms.css' %} ">
<link rel="stylesheet" href="{% static 'plugins/font-awesome/css/font-awesome.min.css' %}"/>
{%endblock%}
{% block content %}
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <!-- BEGIN EXAMPLE TABLE PORTLET-->
            <div class="box box-success">
                <div class="box-header with-border">
                    <div class="caption">
                        <button class="btn btn-primary" onclick="window.location.href = '{% url 'credentiallist' %}'">
                            {%trans 'Return Credential List' %}
                        </button>
                    </div>

                    <div class="box-tools pull-right">
                        <button type="button" class="btn btn-box-tool" data-widget="collapse"><i
                                class="fa fa-minus"></i>
                        </button>
                        <div class="btn-group">
                            <button type="button" class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown">
                                <i class="fa fa-wrench"></i></button>
                            <ul class="dropdown-menu" role="menu">
                                <li><a href="#">{%trans 'Action' %}</a></li>
                                <li><a href="#">{%trans 'Another action' %}</a></li>
                                <li><a href="#">{%trans 'Something else here' %}</a></li>
                                <li class="divider"></li>
                                <li><a href="#">{%trans 'Separated link' %}</a></li>
                            </ul>
                        </div>
                        <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>
                        </button>
                    </div>
                </div>
                <div class="box-body">

                    <div class="row">

                        <div class="col-lg-10">
                            <div class="content float-e-margins">
                                <div class="content">
                                    <form action="{% url 'credentialcreate' %}"
                                          class="form-horizontal nice-validator n-yellow form" id="table"
                                          method="post" novalidate="novalidate">
                                        {% csrf_token %}
                                        <div id='container1' class="table"></div>


                                        <div class="col-sm-4 col-sm-offset-5">
                                            <button type="submit" id="submit" class="btn btn-primary pull-center">
                                                {%trans 'submit' %}
                                            </button>
                                        </div>

                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- END EXAMPLE TABLE PORTLET-->
        </div>

    </div>
{% endblock %}
{% block js %}
<script src="{% static 'plugins/markdown/markdown.js' %} "></script>
<script src="{% static 'plugins/bootstrap-select/js/bootstrap-select.js' %} "></script>
<script src="{% static 'plugins/bootstrap-select/js/i18n/defaults-en_US.js' %} "></script>
<script src="{% static 'plugins/json-forms/js/brutusin-json-forms.js' %} "></script>
<script src="{% static 'plugins/json-forms/js//brutusin-json-forms-bootstrap.js' %} "></script>
<script type="application/javascript">
    var schema = {
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                'title': "{%trans 'credential name' %}",
                "description": "{%trans 'credential name' %}",
                "required": true,
                "minLength": 1,
                "maxLength": 40,
            },
            "protocol": {
                "type": "string",
                'title': "{%trans 'protocol' %}",
                "description": "{%trans 'protocol' %}",
                "required": true,
                "default": "ssh",
                "enum":[
                    "ssh-password",
                    "ssh-key",
                    //"ssh-key-with-password",
                    "vnc",
                    "rdp",
                    "telnet",
                ]
            },
            "port": {
                "type": "number",
                'title': "{%trans 'port' %}",
                "description": "{%trans 'port number' %}",
                "required": true,
                "default": 22,
            },
            "username": {
                "type": "string",
                'title': "{%trans 'username' %}",
                "description": "{%trans 'username name' %}",
                "required": true,
                "minLength": 1,
                "maxLength": 40,
            },
            "method": {
                "dependsOn": [
                    "protocol",
                ]
            },
            "security":{
                "dependsOn": [
                    "protocol",
                ]
            },
            "password": {
                "dependsOn": [
                    "protocol",
                ]
            },
            "key": {
                "dependsOn": [
                    "protocol",
                ]
            },
            "width": {
                "dependsOn": [
                    "protocol",
                ]
            },
            "height": {
                "dependsOn": [
                    "protocol",
                ]
            },
            "dpi": {
                "dependsOn": [
                    "protocol",
                ]
            },
            "proxy": {
                "type": "boolean",
                "title": "{%trans 'proxy' %}",
                "description": "{%trans 'enable proxy' %}",
                "default": false,
                "properties": {
                    "enabled": {
                        "type": "boolean",
                        "title": "{%trans 'Enabled' %}"
                    },
                },
                "required": [
                    "enabled"
                ]
            },
            "proxyserverip": {
                "dependsOn": [
                    "proxy",
                ]
            },
            "proxyport": {
                "dependsOn": [
                    "proxy",
                ]
            },
            "proxypassword": {
                "dependsOn": [
                    "proxy",
                ]
            },
        }
    };

    function resolver (names, data, cb) {
        var schemas = new Object();
        //password property
        var schema = new Object();
        var proxyserveripschema = new Object();
        var proxyportschema = new Object();
        var proxypasswordschema = new Object();
        var methodschema = new Object();
        var widthschema = new Object();
        var heightschema = new Object();
        var dpischema = new Object();
        var portschema = new Object();
        var securityschema = new Object();

        portschema.type = "number";
        portschema.title = "{%trans 'port' %}";
        portschema.description = "{%trans 'port number' %}";
        portschema.required = true;
        portschema.default = 22;

        if (data.protocol == "ssh-password") {

            methodschema = {
                    "type": "string",
                    'title': "{%trans 'method' %}",
                    "description": "{%trans 'method' %}",
                    "required": true,
                    "default": "password",
                    "readOnly": true,
                    "enum":[
                        "password",
                        "key",
                ],
            };
            
            schema.type = "string";
            schema.title = "{% trans 'password' %}";
            schema.format = "password";
            schema.required = true;
            schema.maxLength = 40 ;

            widthschema.type = "null";
            heightschema.type = "null";
            dpischema.type = "null";


        } else if (data.protocol == "ssh-key") {
            methodschema = {
                "type": "string",
                'title': "{%trans 'method' %}",
                "description": "{%trans 'method' %}",
                "required": true,
                "default": "key",
                "readOnly": true,
                "enum":[
                    "password",
                    "key",
                ],
            };
            
            schema.type = "string";
            schema.title = "ssh-key";
            schema.format = "text";
            schema.required = true;

            widthschema.type = "null";
            heightschema.type = "null";
            dpischema.type = "null";

        }
        <!--else if (data.protocol == "ssh-key-with-password") {-->
            <!--methodschema = {-->
                <!--"type": "string",-->
                <!--'title': "{%trans 'method' %}",-->
                <!--"description": "{%trans 'method' %}",-->
                <!--"required": true,-->
                <!--"default": "key",-->
                <!--"readOnly": true,-->
                <!--"enum":[-->
                    <!--"password",-->
                    <!--"key",-->
                <!--],-->
            <!--};-->

            <!--schema.type = "string";-->
            <!--schema.title = "ssh-key";-->
            <!--schema.format = "text";-->
            <!--schema.required = true;-->

            <!--widthschema.type = "null";-->
            <!--heightschema.type = "null";-->
            <!--dpischema.type = "null";-->

        <!--}-->
        else if (data.protocol == "vnc") {
            schema.type = "string";
            schema.title = "{% trans 'password' %}";
            schema.format = "password";
            schema.required = true;
            schema.maxLength = 40 ;

            widthschema = {
                "type": "number",
                'title': "{%trans 'width' %}",
                "description": "{%trans 'width' %}",
                "required": true,
                "default": 1024,
            };
            
            heightschema={
                "type": "number",
                'title': "{%trans 'height' %}",
                "description": "{%trans 'height' %}",
                "required": true,
                "default": 768,
            };
            
            dpischema={
                "type": "number",
                'title': "{%trans 'dpi' %}",
                "description": "{%trans 'dpi' %}",
                "required": true,
                "default": 96,
            };
            
            methodschema = {
                    "type": "string",
                    'title': "{%trans 'method' %}",
                    "description": "{%trans 'method' %}",
                    "required": true,
                    "default": "password",
                    "readOnly": true,
                    "enum":[
                        "password",
                        "key",
                ],
            };
            
            portschema.default = 5901;
        } else if (data.protocol == "rdp") {
            schema.type = "string";
            schema.title = "{% trans 'password' %}";
            schema.format = "password";
            schema.required = true;
            schema.maxLength = 40 ;
            
            widthschema = {
                "type": "number",
                'title': "{%trans 'width' %}",
                "description": "{%trans 'width' %}",
                "required": true,
                "default": 1024,
            };
            
            heightschema={
                "type": "number",
                'title': "{%trans 'height' %}",
                "description": "{%trans 'height' %}",
                "required": true,
                "default": 768,
            };
            
            dpischema={
                "type": "number",
                'title': "{%trans 'dpi' %}",
                "description": "{%trans 'dpi' %}",
                "required": true,
                "default": 96,
            };
            
            methodschema = {
                    "type": "string",
                    'title': "{%trans 'method' %}",
                    "description": "{%trans 'method' %}",
                    "required": true,
                    "default": "password",
                    "readOnly": true,
                    "enum":[
                        "password",
                        "key",
                ],
            };
            securityschema = {
                    "type": "string",
                    'title': "{%trans 'security' %}",
                    "description": "{%trans 'security' %}",
                    "required": true,
                    "default": "any",
                    "readOnly": false,
                    "enum":[
                        "rdp",
                        "nla",
                        "tls",
                        "any"
                ],
            };

            portschema.default = 3389;
        } else {
        //telnet protocol
            schema.type = "string";
            schema.title = "{% trans 'password' %}";
            schema.format = "password";
            schema.required = true;
            schema.maxLength = 40 ;
            
            widthschema = {
                "type": "number",
                'title': "{%trans 'width' %}",
                "description": "{%trans 'width' %}",
                "required": true,
                "default": 1024,
            };
            
            heightschema={
                "type": "number",
                'title': "{%trans 'height' %}",
                "description": "{%trans 'height' %}",
                "required": true,
                "default": 768,
            };
            
            dpischema={
                "type": "number",
                'title': "{%trans 'dpi' %}",
                "description": "{%trans 'dpi' %}",
                "required": true,
                "default": 96,
            };
            
            methodschema = {
                    "type": "string",
                    'title': "{%trans 'method' %}",
                    "description": "{%trans 'method' %}",
                    "required": true,
                    "default": "password",
                    "readOnly": true,
                    "enum":[
                        "password",
                        "key",
                ],
            };
            portschema.default = 23;
        }

        if ( data.proxy ) {
            proxyserveripschema.type = "string";
            proxyserveripschema.title = "{% trans 'proxy server ip' %}";
            proxyserveripschema.format = "ipv4";
            proxyserveripschema.required = true;

            proxyportschema.type = "number";
            proxyportschema.title = "{% trans 'proxy port' %}";
            proxyportschema.required = true;

            proxypasswordschema.type = "string";
            proxypasswordschema.title = "{% trans 'proxy password' %}";
            proxypasswordschema.format = "password";
            proxypasswordschema.required = false;

        } else {
            proxyserveripschema.type = "null";
            proxyportschema.type = "null";
            proxypasswordschema.type = "null";

        };

        schemas["$.password"] = schema;
        schemas["$.proxyserverip"] = proxyserveripschema;
        schemas["$.proxyport"] = proxyportschema;
        schemas["$.proxypassword"] = proxypasswordschema;
        schemas["$.method"] = methodschema;

        schemas["$.security"] = securityschema;

        schemas["$.width"] = widthschema;
        schemas["$.height"] = heightschema;
        schemas["$.dpi"] = dpischema;
        schemas["$.port"] = portschema;

        setTimeout(function(){cb(schemas)},500); // in order to show asynchrony
    }

    var BrutusinForms = brutusin["json-forms"];
    BrutusinForms.bootstrap.addFormatDecorator("inputstream", "file", "glyphicon-search", function (element) {
        alert("user callback on element " + element)
    });
    BrutusinForms.bootstrap.addFormatDecorator("date", "date");
    BrutusinForms.bootstrap.addFormatDecorator("password", "password");
    BrutusinForms.bootstrap.addFormatDecorator("ipv4", "ipv4");
    var bf = BrutusinForms.create(schema);
    bf.schemaResolver = resolver;
    var container = document.getElementById('container1');
    bf.render(container, null);

    $("#submit").on('click', function (e) {
        e.preventDefault();
        var jsondata = bf.getData();
        jsondata["action"] = "create";
        if (jsondata.protocol == 'ssh-password') {
            delete jsondata.key;
        } else if (jsondata.protocol == 'ssh-key'){
            jsondata.key = jsondata.password;
            delete jsondata.password;
        } else{
            delete jsondata.key;
        };
        if (bf.validate()) {
            $.ajax({
                type: "POST",
                url: "{% url 'credentialcreate' %}",
                data: JSON.stringify(jsondata, null, 4),
                dataType: "json",
                success: function (data) {
                    if(data.status){
                        toastr["success"](data.message);
                        setTimeout(function () {
                            location.reload();
                        },2000);
                    } else {
                        toastr["error"](data.message);
                    }
                },
                failure: function (errMsg) {
                    toastr["error"](errMsg);
                }
            });
        }
    });
</script>
<script>
  $("[href='{% url 'credentiallist' %}']").parent("li").addClass('active')
</script>
{% endblock %}